package com.young.customer.utils;

import com.alibaba.fastjson.JSON;
import com.young.customer.config.UrlConstants;
import com.young.customer.domain.SecurityConfDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @auter miaozhenfei
 * @date 2019/7/30 18:31
 */

@Configuration
public class AccessTokenUtil {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
   private HttpClientUtils httpClientUtils;
    @Autowired
    private RedisUtils redisUtils;
    @Value("${platform.platformName}")
    private   String platformName;
    @Value("${platform.passWord}")
    private   String passWord;
    @Value("${platform.secret}")
    private   String secret;
    @Value("${platform.registBusNum}")
    private   String registBusNum;


    public  String getaccessToken() {
        String result  =null;
        String accessToken=null;
        String sign= MD5Util.md5(platformName + registBusNum + passWord + secret);
        try {

            SecurityConfDO securityConf=new SecurityConfDO();
            securityConf.setPlatformName(platformName);
            securityConf.setPassWord(passWord);
            securityConf.setSecret(secret);
            securityConf.setSign(sign);

            result = httpClientUtils.doPost(UrlConstants.ACCESSTOKEN_URL, MapUtils.objectToMap(securityConf));
//            Map<String, Object> map = JsonUtils.jsonToMap(result);
            Map<String,Object> map= (Map<String, Object>) JSON.parseObject(result,Map.class);
            accessToken = (String) map.get("accessToken");
            int  time = (int) map.get("time");
            redisUtils.setKey("goYuongAccessToken", accessToken, time-10);
            System.err.println(accessToken+"-------------accessToken0000000------");

        } catch (Exception e) {
            e.printStackTrace();
        }

        return accessToken;
    }
}
